<%
xml = Builder::XmlMarkup.new(:indent=>2)
xml.instruct!
xml.kml(:xmlns => "http://earth.google.com/kml/2.2") {
  xml.Document {


    # contributors layer
    xml.Folder{
      xml.name("Level of exposure")
      @ranges.each_with_index { |range, i|  
        xml.Folder{
          xml.name("#{range[:label]}")
          range[:segment].each {|r|
            p1=r[:geom].points[0]
            p2=r[:geom].points[1]
            distance=p1.ellipsoidal_distance(p2).to_f
            density=r[:size].to_f/(distance)
            xml.Placemark{
              xml.name("Seg of #{r[:name]} - #{r[:average]} dB(A)")
              xml.description("Segment of the road #{r[:name]}<br/> Gathering #{r[:size]} measures<br/>Aggregated measures: #{r[:loudness]} <br/> Distribution: <img src=\"#{r[:graph_url]}\"> <br/> density: #{density} , distance: #{distance}")
              color=get_color_loudness(r[:average],"FF")
              xml.Style{  
                xml.LineStyle{
                  xml.color(color)
                  xml.colorMode("normal")
                  xml.width("8")
                  #xml.fill(true) #xml.outline(false)
                }
                xml.PolyStyle{
                  xml.color(color)
                  xml.colorMode("normal")
                  xml.outline(1)
                  xml.fill(true)
                }
              }
              # set the color according the loudness set the height according to
              # the intensity of the contribution

              xml.MultiGeometry{
                xml.LineString{
                  xml.extrude(true)
                  xml.tessellate(true)
                  xml.altitudeMode("relativeToGround")
                  coord=""
                  r[:geom].points.each { |point|
                    coord+="#{point.lng},#{point.lat},#{density.to_f*100} \n"
                  }
                  xml.coordinates(coord)
                }
                #xml<< "#{r[:geom].as_kml}"
              }
            }
          }
        }
      }
    }
        #xml.Folder{
        #xml.name("Corrected")
        #@measures.each_with_index { |point, i|
        #xml.Placemark{
        #xml.name("corrected")      
        #xml.description("#{point.location}")
        #xml.Point{
        #xml.coordinates("#{point.corrected.lng},#{point.corrected.lat},#{point.loudness}")
        #}

        #}
        #}
        #}
      }

    }


  %><%= xml.target! %>